<template>
  <div class="login-container" :style="{'backgroundImage': 'url(' + urlBg + ')',}">
    <!-- 登录框 -->
    <div class="login_box">
      <div class="set">
        <a title="点击更换主题背景" @click="gather">
          <i class="iconfont icon-shezhi"></i>
        </a>
      </div>
      <h2 class="headline">Sign in</h2>
      <!-- 登录表单区 引入element ui表单 -->
      <el-form ref="loginFormRef" label-width="0px" :model="loginForm" :rules="loginFormRules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <input v-model="loginForm.username" placeholder="账号" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <input v-model="loginForm.password" type="password" placeholder="密码" />
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <router-link class="message" to="/login">已有账号,立即登录</router-link>
          <el-button type="success" @click="isShow = true" :disabled="!isDisabled">点击验证</el-button>
          <el-button type="success" @click="login" :disabled="isDisabled">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 验证框 -->
    <div class="auto_box" v-if="isShow">
      <Auth @close="close"></Auth>
    </div>
  </div>
</template>

<script>
import Auth from "@/components/auth/index.vue";
export default {
  name: "Login",
  data() {
    return {
      // 显示验证码弹框
      isShow: false,
      // 是否禁用登录按钮
      isDisabled: true,
      // 这是登录表单的数据绑定
      loginForm: {
        username: "",
        password: "",
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名规则
        username: [
          { required: true, message: "请输入登录用户名", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码规则
        password: [
          { required: true, message: "请输入登录用户名", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
      // 如果localstroage有图片地址则用,反之则默认
      urlBg: localStorage.getItem("wallUrlBg")
        ? localStorage.getItem("wallUrlBg")
        : require("@/assets/bg.jpg"),
    };
  },
  components: {
    Auth,
  },
  methods: {
    // 登录效验和请求
    login() {
      let { loginForm } = this;
      // 实行登录预检验 需要在form表单设置一个ref值
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        let result = await this.$API.reqRegister(loginForm);
        if (result.code === 200) {
          this.$message({
            message: "注册成功,即将跳往登录页",
            type: "success",
          });
          // 通过编程式导航跳转到登录页,路由地址是/login
          setTimeout(() => {
            this.$router.push({ name: "/login", params: { first: true } });
          },500);
        } else {
          this.$message({
            message: result.message,
            type: "error",
          });
          console.log(result.message);
        }
      });
    },
    // 子传父关闭验证框的回调
    close(data) {
      this.isShow = data;
      this.isDisabled = false;
    },
    // 收集背景地址回调
    gather() {
      // url正则表达式
      let reg =
        "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
      // new一个匹配url正则的实例
      var re = new RegExp(reg);
      let urlBg = window.prompt("请输入图片地址");
      // 判断是正确的url
      if (re.test(urlBg)) {
        // 放置在local storage
        localStorage.setItem("wallUrlBg", urlBg);
        // 直接赋值,促使页面变化
        this.urlBg = localStorage.getItem("wallUrlBg");
      } else {
        this.$message({
          type: "error",
          message: "url不正确",
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  width: 100%;
  height: calc(100% - 52px);
  background: #ccc;
  background-repeat: no-repeat;
  background-size: cover;
  .login_box {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    box-shadow: 0 15px 25px rgba(0, 0, 0, 0.5);
    background: rgba(0, 0, 0, 0.6);
    /* 让盒子居中对齐 */
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-sizing: border-box;
    .headline {
      text-align: center;
      font-size: 20px;
      font-weight: 700;
      color: white;
    }
    .el-form-item {
      margin-left: 10%;
      input {
        text-indent: 10px;
        width: 85%;
        color: #fff;
        height: 40px;
        font-size: 16px;
        outline: none;
        margin: 0 auto;
        border: 0;
        background: transparent;
        border-bottom: 2px solid #fff;
      }
    }
    .set {
      text-align: right;
      margin-right: 5px;
      i {
        color: #fff;
      }
    }
  }
  .auto_box {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background: #ccc;
    /* 让盒子居中对齐 */
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-sizing: border-box;
  }
  .message {
    color: red;
    font-size: 12px;
    margin-right: 60px;
  }
  .btns {
    margin-top: 45px;
  }
}
</style>